package ai4games_tests.tests;

import game2dai.maths.Geometry2D;
import game2dai.maths.Vector2D;
import processing.core.PApplet;

/**
 * Test the point near line methods in Geometry2D
 * 
 * @author peter
 *
 */
public class PointNearLineTests extends PApplet {

	Vector2D ls, le, p, pol, poil;
	float closeness;
	boolean pnl, pnil;
	int npCol;
	int mode = 1;
	Vector2D t, ti;

	public void setup() {
		size(400, 400);
		cursor(CROSS);
		ls = new Vector2D(121, 322);
		le = new Vector2D(299, 87);
		p = new Vector2D(160, 410);
		pol = new Vector2D();
		poil = new Vector2D();

		Geometry2D.point_to_line_dist(2);
	}

	public void mouseMoved() {
		// Offset the cursor so we can see what is happening
		p.set(mouseX + 20, mouseY + 20);
		t = Geometry2D.point_nearest_line(ls, le, p);
		ti = Geometry2D.point_nearest_infinite_line(ls, le, p);
		if (mode == 0) {
			pnl = Geometry2D.is_point_near_line(ls, le, p, pol);
			pnil = Geometry2D.is_point_near_infinite_line(ls, le, p, poil);
		}
		else {
			pnl = Geometry2D.is_point_near_line(ls, le, p);
			pnil = Geometry2D.is_point_near_infinite_line(ls, le, p);
			if (t != null)
				pol.set(t);
			if (ti != null)
				poil.set(ti);
		}
	}

	public void line(Vector2D vs, Vector2D ve) {
		line((float)vs.x, (float)vs.y, (float)ve.x, (float)ve.y);
	}

	public void ellipse(Vector2D pos, float size) {
		ellipse((float)pos.x, (float)pos.y, size, size);
	}

	public void draw() {
		background(255);
		// Nearness indicators
		noStroke();
		fill(0);
		text("Infinite Line", 20, 20);
		text("Finite Line", 20, 36);
		if (pnil){
			fill(0, 255, 0);
			ellipse(10, 16, 8, 8);
		}
		if (pnl){
			fill(0, 0, 255);
			ellipse(10, 32, 8, 8);  
		}

		if (ti != null) {
			noStroke();
			fill(0);
			text(ti.toShortString(), (float)ti.x+3, (float)ti.y+20);
			// Color of nearest point
			if (t != null)
				fill(0, 0, 255, 32);
			else
				fill(0, 255, 0, 32);
			ellipse(ti, 30);
			strokeWeight(1);
			stroke(160);
			line(ls, ti);
			line(p, ti);
		}
		stroke(64);
		strokeWeight(1.7f);
		line(ls, le);
		  if (ti != null) {
			    noStroke();
			    fill(255, 0, 0);
			    ellipse(p, 5);
			  }
	}

}
